<?php
namespace Admin\Controller;
use Think\Controller;
/**
 * @author xx
 */
class ActivityController extends CommonController {

    public function index(){
    	$this->display();
    }
	public function act_add(){
		$this->instrus = M('instruments')->where(' 1 ')->select();
		$this->display();
	}
	public function lact_add(){
//		$this->instrus = M('instruments')->where(' 1 ')->select();
		$this->act = M('activity')->find(I('aid'));
		$this->display();
	}
	public function lact_info(){
		$lbac = M('lb_activity')->find($_POST['id']);
		$this->lbact = $lbac;
		$this->atime = date('Y-m-d',$lbac['atime']);
		$this->aetime = date('Y-m-d',$lbac['aetime']);
		$this->display();
	}
	/**
	 * 活动列表
	 */
	public function act_list(){
		$s = I('search');
		$st = I('status') ? I('status') : 1;
		$now = time();
    	if($s){//模糊查询
    		$where['title|type'] = array('like','%'.$s.'%');
    	}else{//选择进行中或已结束活动
    		if($st == 1){//正常
				$where = ' status = 1 and aetime >='.$now;
			}else{//大于结束时间或手动结束
				$where = ' status = 2 or aetime <='.$now;
			}
    	}
		$acts = M('activity');
		$count = $acts->where($where)->count();
		$Page   = new \Think\Page($count,10);
		$Page->parameter['search'] = $s;
		$Page->parameter['status'] = $st;
		$show   = $Page->show();
		$act = $acts->where($where)->order(' id desc ')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('act',$act);
        $this->assign('page',$show);
		$this->assign('search',$s);
		$this->assign('status',$st);
		$this->display();
	}
 	/**
	  * 轮播图列表
	  */
	 public function lact_list(){
	 	$now = time();
	 	$st = I('status') ? I('status') : 1;
		if($st == 1){//正常
			$where = ' status = 1 and atime<'.$now.' and aetime>'.$now;
		}else if($st == 2){//结束
			$where = ' status = 2 or aetime<'.$now;
		}else{//未开始
			$where = ' status =1 and atime >='.$now;
		}
		$lbacts = M('lb_activity');
		$count = $lbacts->where($where)->count();
		$Page   = new \Think\Page($count,10);
		$Page->parameter['status'] = $st;
		$this->page = $Page->show();
		$this->lbact = $lbacts->where($where)->order(' numorder asc ')->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->status = $st;
		$this->display();
	 }
	/**
	 * 增加 修改轮播图
	 */
	 public function lact_save(){
	 	if(IS_POST){
	 		M('activity')->where(array('id'=>I('aid')))->setField('islb',1);//修改活动为轮播状态
	 		$_POST['atime']=strtotime($_POST['atime']);
			$_POST['aetime']=strtotime($_POST['aetime']);
		 	$upload = get_upload_img(C("LB_ACTIVITY"));
			$imgurl = $upload['imgurl'];
			$savename = $imgurl['savename'];
			if($imgurl){//有上传图片
					$x = I('dataX');
					$y = I('dataY');
					$w = I('dataWidth');
					$h = I('dataHeight');
					$pre_imgurl = C("LB_ACTIVITY").$imgurl['savepath'].'/'.$imgurl['savename'];//未剪切的图片位置
					$savepath = C('LB_ACTIVITY');//保存相对路径
					get_and_cut_upload_img($pre_imgurl,$savepath,$savename,$x,$y,$w,$h);//剪切图片
					$_POST['imgurl'] = C('TEAL_LB_ACTIVITY').$imgurl['savename'];//保存图片绝对路径
			}
		 	if($_POST['id']){
		 		M('lb_activity')->save($_POST);
		 	}else{
		 		//已有轮播记录并已经结束，在增加之前删除
	 			$lbact = M('lb_activity')->where(array('aid'=>I('aid'),'status'=>2))->find();
				if($lbact){M('lb_activity')->delete($lbact['id']);}
		 		M('lb_activity')->add($_POST);
		 	}
			$this->redirect('lact_list');
		}
	 }
	
	/**
	 * 修改轮播图顺序
	 */
	public function update_umberorder(){
		if(IS_POST){
			$lbact = M('lb_activity')->find($_POST['id']);
			$lbact['numorder'] = $_POST['numorder'];
			if(M('lb_activity')->save($lbact)){
				$this->ajaxReturn(1);
			}
		}
	}
	/**
	 * 增加与修改非主题图片信息
	 */
	public function act_save_nthemeimg(){
		if(IS_POST){
			$_POST['atime']=strtotime($_POST['atime']);
			$_POST['aetime']=strtotime($_POST['aetime']);
			$_POST['content'] = htmlspecialchars_decode($_POST['content']);
			if($_POST['id']){
				$aid = $_POST['id'];
				M('activity')->save($_POST);
				if($_POST['instru']){
					M('activity_instrument')->where(array('aid'=>$aid))->delete();
					$instru = $_POST['instru'];
					foreach($instru as $v){
						$act_instru['aid']=$aid;
						$act_instru['instrumentid'] = $v;
						M('activity_instrument')->add($act_instru);
					}
				}
			}else{//增加
				$aid = M('activity')->add($_POST);
				$acturl = C('WWW_URL_PRE').__ROOT__.'/Activity/act_detail/id/'.$aid.'/shareaid/'.$aid.'/isshare/'.'1';
				$actshareurl = C('WWW_URL_PRE').__ROOT__.'/User/joinact/aid/'.$aid.'/shareaid/'.$aid.'/isshare/'.'1';
				M('activity')->where(array('id'=>$aid))->setField('acturl',$acturl);
				M('activity')->where(array('id'=>$aid))->setField('actshareurl',$actshareurl);
				$instru = $_POST['instru'];
				foreach($instru as $v){
					$act_instru['aid']=$aid;
					$act_instru['instrumentid'] = $v;
					M('activity_instrument')->add($act_instru);
				}
			}
			$this->ajaxReturn(array('aid'=>$aid));
		}
	}
	/**
	 * 修改主题图片
	 */
	 public function act_save_themeimg(){
	 	if(IS_POST){
	 		$aid = $_POST['aid'];
			$activity = M('activity')->where(array('id'=>$aid))->find();
			$upload = get_upload_img(C('ACTIVITY'));
			$theme = $upload['theme'];
			$savename = $theme['savename'];
			if($upload){//有上传图片
				$x = I('dataX');
				$y = I('dataY');
				$w = I('dataWidth');
				$h = I('dataHeight');
				$pre_imgurl = C('ACTIVITY').$theme['savepath'].'/'.$theme['savename'];//未剪切的图片位置
				$savepath = C('ACTIVITY_THEME');//保存相对路径
				get_and_cut_upload_img($pre_imgurl,$savepath,$savename,$x,$y,$w,$h);//剪切图片
				$activity['theme'] = C('TEAL_ACTIVITY_THEME').$theme['savename'];//保存图片绝对路径
				M('activity')->save($activity);
			}
			$this->redirect('act_list');
	 	}
	 }
	 
	/**
     *  活动信息
     *  @author xx
     */
    public function act_info() {
    	$id = I('id');
		$activity = M('activity');
    	if($id){
    		$act = $activity->find($id);
			$act_instru = D('ActivityInstrumentListView')->where(array('aid'=>$id))->select();
			$this->instrus = M('instruments')->where(' 1 ')->select();//所有乐器
			$this->act_instru = $act_instru;
			$this->assign('act',$act);
			$this->assign('atime',date('Y-m-d H:i',$act['atime']));
			$this->assign('aetime',date('Y-m-d H:i',$act['aetime']));
		}
		$this->display();
    }
	/**
	 * 活动所有信息，包括报名信息
	 *  @author xx
	 */
	 public function act_allinfo(){
	 	$id = I('id');//活动ID
		$activity = M('activity');
		$act = $activity->find($id);
		$activity_event = D('ActivityEventListView')->where(array('aid'=>$id,'status'=>1))->select();
		$act_instru = D('ActivityInstrumentListView')->where(array('aid'=>$id))->select();
		$act_cooper = D('ActivityCooperListView')->where(array('aid'=>$id))->select();
		$this->act=$act;//活动内容
		$this->act_instru = $act_instru;//活动可用器材
		$this->act_event=$activity_event;//活动报名情况
		$this->cooper = $act_cooper;//活动合作情况
		$this->assign('atime',date('Y-m-d H:i',$act['atime']));
		$this->assign('aetime',date('Y-m-d H:i',$act['aetime']));
        $this->display();
	 }
	/**
	 * 取消用户报名报名
	 */
	 public function cancle_act(){
		if(I('id')){
			$act_event = M('activity_event')->where(array('id'=>I('id')))->find();
			$aid = $act_event['aid'];
			$uid = $act_event['uid'];
			$act = M('activity')->where(array('id'=>$aid))->find();
			M('activity_event')->where(array('id'=>I('id')))->setField('status',0);
			//发送站内信至指定用户
			$inform['uid'] = $uid;
			$inform['content'] = '已取消活动'.$act['title'].'的报名';
			$inform['time'] = time();
			M('inform')->add($inform);
			$this->ajaxReturn(1);
		}
	}
	 /**
	  * 活动合作信息页面
	  */
	 public function act_cooper_info(){
	 	$id = I('id');//活动ID
	 	$act = M('activity')->find($id);
		$cooper = D('ActivityCooperListView')->where(array('aid'=>$id))->select();
		$this->cooper = $cooper;
		$this->act = $act;
		$this->assign('atime',date('Y-m-d H:i',$act['atime']));
		$this->assign('aetime',date('m-d H:i',$act['aetime']));
		$this->display();
	 }
	 /**
	  * 活动合作信息增加与修改
	  */
	  public function add_cooper(){
	  	$aid = intval(I('aid'));
		$id = M('activity_cooper')->add($_POST);
		$cooper = M('activity_cooper')->where(array('id'=>$id))->find();
		$upload = get_upload_img(C('ACTIVITY_COOPER'),$aid.'/');
		$logourl = $upload['logourl'];
		$cooper['logourl'] = C('TEAL_ACTIVITY_COOPER').$logourl['savepath'].$logourl['savename'];
		if(M('activity_cooper')->save($cooper)){
			$this->redirect('Activity/act_cooper_info',array('id'=>$aid));
		}else{
			$this->error('添加活动合作信息失败');
		}
	  }
	 /**
	  * 删除活动合作信息
	  */
	  public function delete_cooper(){
	  	$id = intval(I('id'));
		$cooper =M('activity_cooper')->where(array('id'=>$id))->find();
		if(M('activity_cooper')->where(array('id'=>$id))->delete()){
			unlink($cooper['logourl']);
			$this->ajaxReturn(1);
		}
	  }
	 /**
	  * 删除活动
	  */
	  public function act_delect(){
	  	$id = I('id');
	  	if($id){
	  		if(M('activity')->delete($id)){
	  			$this->success('删除成功');
	  		}
		}
	  }
	  /**
	  * 删除轮播活动
	  */
	  public function lact_delect(){
	  	$id = I('id');
	  	if($id){
	  		if(M('lb_activity')->delete($id)){
	  			$this->success('删除成功');
	  		}
		}
	  }
	  /**
	   * 活动下架
	   */
	  public function act_soldout(){
	   	$id = I('id');
	  	if($id){
	  		if(M('activity')->save( array( 'id' => $id , 'status' => 2 ) )){
	  			$this->success('活动已结束');
	  		}
		}
	  }
	  /**
	   * 轮播活动下架
	   */
	  public function lact_soldout(){
	   	$id = I('id');
	  	if($id){
	  		$lbact = M('lb_activity')->where(array('id'=>$id))->find();
			$act = M('activity')->where(array('id'=>$lbact['aid']))->find();
	  		if(I('status')){
	  			//还原时，轮播开始时间变成今天 结束时间变成明天
	  			$aetime=time()+60*60*24;
				$lbact_array = array( 'id' => $id,'status' => I('status'),'atime'=>time(),'aetime'=>$aetime);
	  			$lb = M('lb_activity')->save($lbact_array);
				/////////////////////////////
				//轮播对应的活动变成已是轮播状态
				if($act){$act['islb'] = 1;$a = M('activity')->save($act);}
	  			if($lb){
	  				$this->success('轮播已还原');
	  			}
	  		}else{
	  			if($act){$act['islb'] = 0;$a = M('activity')->save($act);}
		  		if(M('lb_activity')->save( array( 'id' => $id , 'status' => 2 ) )){
		  			$this->success('轮播已结束');
		  		}
			}
		}
		if(I('aid')){
			$act = M('activity')->where(array('id'=>I('aid')))->find();
			$lbact = M('lb_activity')->where(array('aid'=>I('aid')))->find();
			$act['islb'] = 0;
			$lbact['status'] = 2;
			if(M('activity')->save($act) && M('lb_activity')->save($lbact)){
				$this->success('轮播已结束');
			}
		}
	  }
	  /**
	   * 取得一个活动中的任一个用户的报名曲目
	   */
	  public function get_music($aid,$uid){
	  	$act_user_music = D('ActivityEventMusicView')->where(array('aid'=>$aid,'uid'=>$uid))->select();
		$music = '';
		foreach($act_user_music as $v){
			$music = $music.$v['musicname'].'('.$v['musictype'].')  ';
		}
		return $music;
	  }
	  /**
	   * 取得一个活动中任一个用户的报名选择音乐器材
	   */
	   public function get_instrument($aid,$uid){
	   	$act_user_instrument = D('ActivityEventInstrumentView')->where(array('aid'=>$aid,'uid'=>$uid))->select();
		   	$instrus = '';
			foreach($act_user_instrument as $v){
				$instrus = $instrus.$v['instru_name'].'  ';
			}
			return $instrus;
	   }
	  
	 /**
	 * 导出活动Excel
	 */
	public function out_actusers_excel(){
		$id = I('id');
		$act_event = D('ActivityEventListView')->where(array('aid'=>$id))->select();
		$act = M('activity')->where(array('id'=>$id))->find();
		$excel_obj = new \Common\Api\Excel();
		$excel_data = array();	
		$excel_obj->setStyle(array('id'=>'s_title','Font'=>array('FontName'=>'宋体','Size'=>'14','Bold'=>'1')));	
		$excel_data[0][] = array('styleid'=>'s_title','data'=>'活动时间');
		$excel_data[1][] = array('styleid'=>'s_title','data'=>change_time($act['atime']));
		$excel_data[0][] = array('styleid'=>'s_title','data'=>'活动名');
		$excel_data[1][] = array('styleid'=>'s_title','data'=>$act['title']);
		$excel_data[2][] = array('styleid'=>'s_title','data'=>' ');
		$excel_data[3][] = array('styleid'=>'s_title','data'=>'报名详情');
		$excel_data[4][] = array('styleid'=>'s_title','data'=>'姓名');
		$excel_data[4][] = array('styleid'=>'s_title','data'=>'手机');
		$excel_data[4][] = array('styleid'=>'s_title','data'=>'表演类型');
		$excel_data[4][] = array('styleid'=>'s_title','data'=>'乐队名');
		$excel_data[4][] = array('styleid'=>'s_title','data'=>'报名人数');
		$excel_data[4][] = array('styleid'=>'s_title','data'=>'使用乐器');
		$excel_data[4][] = array('styleid'=>'s_title','data'=>'报名歌曲');
		foreach($act_event as $v){
			$tmp = array();
			$tmp[] = array('data'=>$v['name']);
			$tmp[] = array('data'=>$v['phone']);
			$tmp[] = array('data'=>$v['type']);
			$tmp[] = array('data'=>$v['bandname']);
			$tmp[] = array('data'=>$v['number']);
			$tmp[] = array('data'=>$this->get_instrument($v['aid'], $v['uid']));
			$tmp[] = array('data'=>$this->get_music($v['aid'], $v['uid']));
			$excel_data[] = $tmp;
		}
		$excel_data = $excel_obj->charset($excel_data,'utf-8');
		$excel_obj->addArray($excel_data);
		//控制列的长度
		$column = array(array('column'=>1,'width'=>180),array('column'=>2,'width'=>80),array('column'=>3,'width'=>80),array('column'=>4,'width'=>100),array('column'=>5,'width'=>100),array('column'=>6,'width'=>180),array('column'=>7,'width'=>180));
		$excel_obj->addWorksheet($excel_obj->charset('活动详情','utf-8'),$column);
		$excel_obj->generateXML($excel_obj->charset($act['title'],'utf-8'));
	}	
	
}
